<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

class AdminLog extends Base {
    public function getList($where = [], $order = '', $limit = 10, $field = '', $out = false){
        $list = $this
            ->field($field,$out)
            ->with(['admin','groups'])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $page = $list->currentPage();
        $total = $list->total();
        foreach ($list as $k => $v){
            $list[$k]['sort_asc'] = $k + 1 + ($page-1) * $limit;
            $list[$k]['sort_desc'] = $total - ($page-1) * $limit - $k;
        }
        return $list;
    }

    public function getOneById($id = 0, $field = '*'){
        $info = $this
            ->field($field)
            ->with('admin')
            ->find($id);
        return $info;
    }

    public function add($data){
        $this->save($data);
    }

    public function admin(){
        return $this->hasOne('Admin','id','aid')
            ->bind([
                'username',
                'nickname',
                'gid'
            ]);
    }

    public function groups(){
        return $this->hasOne('Group','id','gid')
            ->bind([
                'group_name',
                'title'
            ]);
    }
}